package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.Comment;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *   凑对去评论-列表信息异步请求
 */
public class GetTourCommentLIst extends BaseCommand {

    //mid=31&itemid=1
    private String mid = "31";
    private String itemid;

    private int page =  DataCenter.LISTVIEW_DEFAULT_PAGE; //当前页码
    private static final String TAG = "GetTourComment";

    public GetTourCommentLIst(String itemid) {
        this.itemid = itemid;
    }

    public GetTourCommentLIst(String itemid, int page) {
        this.itemid = itemid;
        this.page = page;
    }

    public GetTourCommentLIst(String mid ,String itemid, int page) {
        this.mid = mid;
        this.itemid = itemid;
        this.page = page;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_TOUT_comment_ADDRESS;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("mid", mid);
        paramters.put("page",page + "");
        paramters.put("itemid", itemid);
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetTourComment.yes()");
        JSONArray jsonArray = null;
        try {
            Log.d(TAG, result.object.toString());

            JSONObject jsonObject = new JSONObject(result.object.toString());
            jsonArray = jsonObject.optJSONArray("data");

            ArrayList<Comment> targetList = DataCenter.tourCommentList;
            if (page == DataCenter.LISTVIEW_DEFAULT_PAGE) {
                targetList.clear();
            }

            for(int i = 0; i < jsonArray.length(); ++i){ // i从0开始，屏蔽提示信息
                JSONObject o = jsonArray.getJSONObject(i);
                Comment bean = JsonUtil.jsonToObject(o.toString(), Comment.class);
                targetList.add(bean);
            }
            Log.d(TAG, targetList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }


        if (jsonArray.length() < 1) {
            DataCenter.getInstance().callBacks(DataCenter.flag.GET_TOUR_COMMENT_LSIT_NO_MORE_DATA);
        } else {
            DataCenter.getInstance().callBacks(DataCenter.flag.GET_TOUR_COMMENT_LIST_SUCESS);
        }
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "GetTourComment.no()");
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_TOUR_COMMENT_LSIT_Failed);

    }
}